package cs5226.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import cs5226.model.*;

public class RedoLogDAO extends AWRStatisticsDAO {
	public static List<PercentageSingleValueStatistics> getRedoAllocationHit()
			throws SQLException {
		String exprStat = "(100* (sum(decode(a.stat_name,'redo entries', a.value)) - " +
				" sum(decode(a.stat_name,'redo log space requests', a.value))) / " +
				" sum(decode(a.stat_name,'redo entries', a.value)))";
		String awrTable = "DBA_HIST_SYSSTAT";
		String filter = "a.stat_name in ('redo log space requests', 'redo entries')";
		
		return searchAWRStatistics(PercentageSingleValueStatistics.class, exprStat, awrTable, DAYSLATEST, filter);
	}
	
	public static List<PercentageSingleValueStatistics> getRedoAllocationHit(String fromDate, String toDate)
			throws SQLException {
		String exprStat = "(100* (sum(decode(a.stat_name,'redo entries', a.value)) - " +
				" sum(decode(a.stat_name,'redo log space requests', a.value))) / " +
				" sum(decode(a.stat_name,'redo entries', a.value)))";
		String awrTable = "DBA_HIST_SYSSTAT";
		String filter = "a.stat_name in ('redo log space requests', 'redo entries')";
		
		return searchAWRStatisticsWithinAPeriod(fromDate, toDate, PercentageSingleValueStatistics.class, exprStat, awrTable, DAYSLATEST, filter);
	}
	
	public static List<PercentageSingleValueStatistics> getRedoAllocationHitLatest()
			throws SQLException {
		String exprStat = "(100* (sum(decode(a.stat_name,'redo entries', a.value)) - " +
				" sum(decode(a.stat_name,'redo log space requests', a.value))) / " +
				" sum(decode(a.stat_name,'redo entries', a.value)))";
		String awrTable = "DBA_HIST_SYSSTAT";
		String filter = "a.stat_name in ('redo log space requests', 'redo entries')";
		
		return searchAWRStatisticsLatest(PercentageSingleValueStatistics.class, exprStat, awrTable, DAYSLATEST, filter);
	}

	public static List<SingleValueStatistics> getRedoLogSwitchIncompleteWait()
			throws SQLException {
		String exprStat = "sum (TIME_WAITED_MICRO)";
		String awrTable = "DBA_HIST_SYSTEM_EVENT";
		String filter = "a.event_name like 'log file switch (checkpoint incomplete)'";
		
		return searchAWRStatistics(SingleValueStatistics.class, exprStat, awrTable, DAYSLATEST, filter);
	}
	
	public static List<SingleValueStatistics> getRedoLogSwitchIncompleteWait(String fromDate, String toDate)
			throws SQLException {
		String exprStat = "sum (TIME_WAITED_MICRO)";
		String awrTable = "DBA_HIST_SYSTEM_EVENT";
		String filter = "a.event_name like 'log file switch (checkpoint incomplete)'";
		
		return searchAWRStatisticsWithinAPeriod(fromDate, toDate, SingleValueStatistics.class, exprStat, awrTable, DAYSLATEST, filter);
	}
	
	public static List<SingleValueStatistics> getRedoLogSwitchIncompleteWaitLatest()
			throws SQLException {
		String exprStat = "sum (TIME_WAITED_MICRO)";
		String awrTable = "DBA_HIST_SYSTEM_EVENT";
		String filter = "a.event_name like 'log file switch (checkpoint incomplete)'";
		
		return searchAWRStatisticsLatest(SingleValueStatistics.class, exprStat, awrTable, DAYSLATEST, filter);
	}
}
